<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author FIRE
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    showFooter="true"
    :okAuth="'shop:notice:shopNotice:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="60%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="pr-1 m-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm">
      <template #content="{ model, field }">
        <WangEditor
          v-model:value="model[field]"
          :bizKey="record.id"
          :bizType="'shopNotice_' + field"
          :height="500"
        />
      </template>
    </BasicForm>
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsShopNoticeShopNoticeForm',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { ShopNotice, shopNoticeSave, shopNoticeForm } from '/@/api/shop/notice/shopNotice';
  import { WangEditor } from '/@/components/WangEditor';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('shop.notice.shopNotice');
  const { showMessage } = useMessage();
  const record = ref<ShopNotice>({} as ShopNotice);
  const getTitle = computed(() => ({
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: record.value.isNewRecord ? t('新增公告管理') : t('编辑公告管理'),
  }));

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('公告类型'),
      field: 'ctype',
      component: 'Select',
      componentProps: {
        maxlength: 10,
        dictType: 'shop_notice_type',
      },
      required: true,
    },
    {
      label: t('公告标题'),
      field: 'ctitle',
      component: 'Input',
      componentProps: {
        maxlength: 200,
      },
      required: true,
    },
    {
      label: t('内容简介'),
      field: 'simpleContent',
      component: 'InputTextArea',
      componentProps: {
        maxlength: 500,
        rows: 5,
      },
      required: true,
    },
    {
      label: t('内容详情'),
      field: 'content',
      component: 'InputTextArea',
      componentProps: {
        maxlength: 4000,
      },
      required: true,
      slot: 'content',
      colProps: { lg: 24, md: 24 },
    },
    {
      label: t('公告说明'),
      field: 'remarks',
      component: 'InputTextArea',
      componentProps: {
        maxlength: 500,
        rows: 6,
      },
      colProps: { lg: 24, md: 24 },
    },
    {
      label: t('封面上传'),
      field: 'dataMap',
      component: 'Upload',
      componentProps: {
        loadTime: computed(() => record.value.__t),
        bizKey: computed(() => record.value.id),
        bizType: 'shopNotice_image',
        uploadType: 'image',
        maxNumber: 2,
        multiple: false,
      },
      colProps: { lg: 24, md: 24 },
    },
    {
      label: t('封面图地址'),
      field: 'picUrl',
      component: 'Input',
      componentProps: {
        maxlength: 4000,
      },
      dynamicDisabled: true,
    },
    {
      label: t('APP发布'),
      field: 'bappShow',
      component: 'Select',
      componentProps: {
        dictType: 'sys_yes_no',
        allowClear: true,
      },
      dynamicDisabled: true,
    },
    {
      label: t('发布日期'),
      field: 'fbDate',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm',
        showTime: { format: 'HH:mm' },
      },
      dynamicDisabled: true,
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate, updateSchema }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 24, md: 24 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await shopNoticeForm(data);
    record.value = (res.shopNotice || {}) as ShopNotice;
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
    updateSchema([
      {
        field: 'ctype',
        componentProps: {
          disabled: record.value.bappShow == 1 ? true : false,
        },
      },
      {
        field: 'ctitle',
        componentProps: {
          disabled: record.value.bappShow == 1 ? true : false,
        },
      },
      {
        field: 'simpleContent',
        componentProps: {
          disabled: record.value.bappShow == 1 ? true : false,
        },
      },
      {
        field: 'content',
        componentProps: {
          disabled: record.value.bappShow == 1 ? true : false,
        },
      },
      {
        field: 'remarks',
        componentProps: {
          disabled: record.value.bappShow == 1 ? true : false,
        },
      },
      {
        field: 'dataMap',
        componentProps: {
          disabled: record.value.bappShow == 1 ? true : false,
        },
      },
      {
        field: 'picUrl',
        componentProps: {
          disabled: record.value.bappShow == 1 ? true : false,
        },
      },
    ]);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        id: record.value.id,
      };
      // console.log('submit', params, data, record);
      const res = await shopNoticeSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
